Trò chơi Tic-Tac-Toe, game đánh caro full source code
- PickupItemSimple.cs
- UtilityScripts /
- Photon Unity Networking /
- Assets /
- project /
2 using System.Collections;
3
4 /// <summary>
5 /// Makes a scene object pickup-able. Needs a PhotonView which belongs to the scene.
6 /// </summary>
7 [RequireComponent(typeof(PhotonView))]
8 public class PickupItemSimple : Photon.MonoBehaviour
9 {
10 public float SecondsBeforeRespawn = 2;
11 public bool PickupOnCollide;
12 public bool SentPickup;
13
14 public void OnTriggerEnter(Collider other)
15 {
16 // we only call Pickup() if "our" character collides with this PickupItem.
17 // note: if you "position" remote characters by setting their translation, triggers won't be hit.
18
19 PhotonView otherpv = other.GetComponent<PhotonView>();
20 if (this.PickupOnCollide && otherpv != null && otherpv.isMine)
21 {
22 //Debug.Log("OnTriggerEnter() calls Pickup().");
23 this.Pickup();
24 }
25 }
26
27 public void Pickup()
28 {
29 if (this.SentPickup)
30 {
31 // skip sending more pickups until the original pickup-RPC got back to this client
32 return;
33 }
34
35 this.SentPickup = true;
36 this.photonView.RPC("PunPickupSimple", PhotonTargets.AllViaServer);
37 }
38
39 [RPC]
40 public void PunPickupSimple(PhotonMessageInfo msgInfo)
41 {
42 // one of the messages might be ours
43 // note: you could check "active" first, if you're not interested in your own, failed pickup-attempts.
44 if (this.SentPickup && msgInfo.sender.isLocal)
45 {
46 if (this.gameObject.GetActive())
47 {
48 // picked up! yay.
49 }
50 else
51 {
52 // pickup failed. too late (compared to others)
53 }
54 }
55
56 this.SentPickup = false;
57
58 if (!this.gameObject.GetActive())
59 {
60 Debug.Log("Ignored PU RPC, cause item is inactive. " + this.gameObject);
61 return;
62 }
63
64
65 // how long it is until this item respanws, depends on the pickup time and the respawn time
66 double timeSinceRpcCall = (PhotonNetwork.time - msgInfo.timestamp);
67 float timeUntilRespawn = SecondsBeforeRespawn - (float)timeSinceRpcCall;
68 //Debug.Log("msg timestamp: " + msgInfo.timestamp + " time until respawn: " + timeUntilRespawn);
69
70 if (timeUntilRespawn > 0)
71 {
72 // this script simply disables the GO for a while until it respawns.
73 this.gameObject.SetActive(false);
74 Invoke("RespawnAfter", timeUntilRespawn);
75 }
76 }
77
78 public void RespawnAfter()
79 {
80 if (this.gameObject != null)
81 {
82 this.gameObject.SetActive(true);
83 }
84 }
85 }
Makes a scene object pickup-able. Needs a PhotonView which belongs to the scene.
we only call Pickup() if "our" character collides with this PickupItem.
note: if you "position" remote characters by setting their translation, triggers won't be hit.
Debug.Log("OnTriggerEnter() calls Pickup().");
skip sending more pickups until the original pickup-RPC got back to this client
one of the messages might be ours
note: you could check "active" first, if you're not interested in your own, failed pickup-attempts.
picked up! yay.
pickup failed. too late (compared to others)
how long it is until this item respanws, depends on the pickup time and the respawn time
Debug.Log("msg timestamp: " + msgInfo.timestamp + " time until respawn: " + timeUntilRespawn);
this script simply disables the GO for a while until it respawns.